#######################   ecom-analysis.R   ###################################
# Replication code for:
# Using Earnings Calls to Understand the Political Behavior of Major Polluters                      
#
#                      version Aug 12 2021                        
#                                                            
#
#                                                                            
###############################################################################

rm(list = ls())

# Load packages
library(tidyverse)
library(ggrepel)
library(viridis)
library(readxl)

# Load annual ECOM data
polstd <- read.csv("ecom-annual-data.csv")

# Loading Influence Map data
imdat <- read_xlsx("influencemap.xlsx", sheet = "values")

# Adding Europe/N.Am. indicator
europe <- c("BP","Eni","Repsol","Shell","Statoil/Equinor","Total")
polstd$Europe <- 1*(polstd$Firm %in% europe)

polstd <- data.frame(polstd, stringsAsFactors = FALSE)

# Short firm names
polstd$Firm <- plyr::revalue(polstd$Firm, c("Occidental Petroleum" = "Oxy",
                                            "ConocoPhillips" = "Conoco",
                                            "ExxonMobil" = "Exxon",
                                            "Statoil/Equinor" = "Equinor"))


# --------------------------------------------------------------------------- #
#     FIGURE 1: Plotting indicators by year
# --------------------------------------------------------------------------- #

# Yearly averages for indicator
yearavg.w <- summarize(group_by(polstd, Year), 
                       End_FF = mean((endff), na.rm = T) - 2,
                       Climate_science = mean((climate), na.rm = T) - 2,
                       Carbon_price = mean((co2price), na.rm = T) - 2,
                       Intl_agreements = mean((intlagree), na.rm = T) - 2,
                       Natl_laws = mean((natlaws), na.rm = T) - 2,
                       CCS = mean((ccs), na.rm = T) - 2)                    

# Converting to panel data
yearavg.l <- gather(yearavg.w, indicator, stdvalue, End_FF:CCS, factor_key = T)

# Indicator labels
earnings.labs <- c("End Fossil Fuels","Climate science","Carbon price",
                   "Intl Agreements","National Laws","CCS")
names(earnings.labs) <- c("End_FF","Climate_science","Carbon_price",
                          "Intl_agreements","Natl_laws","CCS")

# Plot
ggplot(yearavg.l, 
       aes(x = Year, y = stdvalue)) + 
  geom_line() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_x_continuous(breaks = c(2005,2010,2015,2019), labels = c("'05","'10","'15","'19")) + 
  scale_y_continuous(breaks = seq(-1,1,0.5), name = "Rescaled index (-1: anti-climate; +1: pro-climate)") +
  coord_cartesian(ylim = c(-1,1)) + 
  ggtitle("Earnings calls indicators, annual averages (2005-2019)") + 
  facet_wrap( ~ indicator, nrow = 2, labeller = labeller(indicator = earnings.labs)) + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.text.x = element_text(size=8), 
        axis.text.y = element_text(size=8), 
        title = element_text(size=9))
# ggsave("Earnings_yearly.pdf", width=6, height=5)



# --------------------------------------------------------------------------- #
#     FIGURE 2: Plotting indicators by firm
# --------------------------------------------------------------------------- #

# Fixing labeling values
polstd$political.labs <- polstd$political
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="Equinor"] <- 0.86
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="BP"] <- 0.8
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="Oxy"] <- 0.13
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="Total"] <- 0.2
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="Repsol"] <- 0.27
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="Chevron"] <- (-0.2)
polstd$political.labs[polstd$Year==2019 & polstd$Firm=="Conoco"] <- (-0.15)

# Color palette
col.pal <- c("#00b159","#d11141","#AC88FF","#f37735","#00aedb",
             "#cccccc","#000000","#8c8c8c","#ffc425","#FF6C90")

ggplot(polstd, 
       aes(x = Year, y = political, group = Firm, col = factor(Firm))) + 
  geom_line() +
  scale_x_continuous(breaks = c(2005,2010,2015,2019), 
                     labels = c("'05","'10","'15","'19")) + 
  scale_y_continuous(breaks = seq(-1,1,0.5), 
                     name = "<--- (anti-climate)  Rescaled index  (pro-climate) --->") +
  scale_color_manual(values = col.pal) + 
  geom_text(data = polstd[polstd$Year==2019,],
            aes(x = Year + 0.25, y = political.labs, label = Firm),
            hjust = 0,
            size = 3) +
  coord_cartesian(ylim = c(-1,1),xlim=c(2005,2020)) + 
  ggtitle("Earnings calls index, firm averages (2005-2019)") + 
  theme(legend.position = "none",
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.text.x = element_text(size=8), 
        axis.text.y = element_text(size=8), 
        title = element_text(size=9))
# ggsave("Earnings_firm.pdf", width=6.5, height=5.5)




# --------------------------------------------------------------------------- #
#     FIGURE 3: Comparing ECOM to InfluenceMap
# --------------------------------------------------------------------------- #

# Re-scaling IM_Policy to numeric (A-B is pro climate policy, C-D-E-F is against)
# Only have D-F for these firms, so coding along with D+ as highest and F- as lowest
# D+ = 9, D = 8, D- = 7
# E+ = 6, E = 5, E- = 4,
# F+ = 3, F = 2, F- = 1

imdat$IM_Policy_n <- ifelse(imdat$IM_Policy=="D+", 9,
                            ifelse(imdat$IM_Policy=="D", 8,
                                   ifelse(imdat$IM_Policy=="D-", 7,
                                          ifelse(imdat$IM_Policy=="E+", 6,
                                                 ifelse(imdat$IM_Policy=="E", 5,
                                                        ifelse(imdat$IM_Policy=="E-", 4,
                                                               ifelse(imdat$IM_Policy=="F", 2, NA)))))))


# Keeping only variables for comparison
polnums <- polstd[ , c("Firm","Year","climate","intlagree","natlaws",
                       "co2price","ccs","endff","political")]

# Short firm names
imdat$Firm <- plyr::revalue(imdat$Firm, c("Occidental Petroleum" = "Oxy",
                                          "ConocoPhillips" = "Conoco",
                                          "ExxonMobil" = "Exxon",
                                          "Statoil/Equinor" = "Equinor"))

# Merging ECOM and InfluenceMap
polmrg <- merge(polnums, imdat, by = c("Firm","Year"), all.x = T, all.y = F)


# FIGURE 3a: IM Climate policy 
ggplot(polmrg %>% filter(!is.na(IM_Policy_n)), 
       aes(x = political, y = IM_Policy_n, label = paste(Firm, Year, sep = "-"))) +
  geom_smooth(method = "lm", se = F, col = "darkgray") +
  geom_point() +
  geom_text_repel(size = 3) +
  scale_y_continuous(breaks = seq(2,8,1),
                     labels = c("F","F+","E-","E","E+","D-","D")) +
  labs(x = "Earning calls index", y = "InfluenceMap Policy Grade") +
  coord_cartesian(xlim = c(-0.3, 1.1)) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.text.x = element_text(size=8), 
        axis.text.y = element_text(size=8), 
        title = element_text(size=9))
# ggsave("IM_policy.pdf", width=6.5, height=5.5)


# FIGURE 3b: IM Brand / PR
ggplot(polmrg %>% filter(!is.na(IM_Brand)), 
       aes(x = political, y = IM_Brand, label = paste(Firm, Year, sep = "-"))) +
  geom_smooth(method = "lm", se = F, col = "darkgray") +
  geom_point() +
  geom_text_repel(size = 3) +
  labs(x = "Earning calls index", y = "InfluenceMap Climate-related PR Spend ($m)") +
  coord_cartesian(xlim = c(-0.1, 0.4)) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.text.x = element_text(size=8), 
        axis.text.y = element_text(size=8), 
        title = element_text(size=9))
# ggsave("IM_Brand.pdf", width=6.5, height=5.5)


# FIGURE 3c: IM Lobbying
ggplot(polmrg %>% filter(!is.na(IM_Lobby)), 
       aes(x = political, y = IM_Lobby, label = paste(Firm, Year, sep = "-"))) +
  geom_smooth(method = "lm", se = F, col = "darkgray") +
  geom_point() +
  geom_text_repel(size = 3) +
  labs(x = "Earning calls index", y = "InfluenceMap Total Lobbying Spend ($m)") +
  coord_cartesian(xlim = c(-0.2, 0.4)) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'), 
        axis.text.x = element_text(size=8), 
        axis.text.y = element_text(size=8), 
        title = element_text(size=9))
# ggsave("IM_lobbying.pdf", width=6.5, height=5.5)


# Correlations
cor.test(polmrg$political, polmrg$IM_Policy_n, method = "pearson")
cor.test(polmrg$political, polmrg$IM_Brand, method = "pearson")
cor.test(polmrg$political, polmrg$IM_Lobby, method = "pearson")

